<template>
  <!-- 客户管理表单 -->
  <el-dialog :visible.sync="localVisible" :title="title" width="500px" @close="handleClose"
    :close-on-click-modal="false" append-to-body>
    <el-form label-width="120px" :model="form" :rules="rules" ref="addCustomer">
      <el-form-item prop="name" label="班级名称">
        <el-input v-model="form.name" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="课程类型" prop="fk_t_obj_type_id">
        <el-select v-model="form.fk_t_obj_type_id" placeholder="请选择" size="mini" style="width: 300px">
          <el-option v-for="item in objTypes" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="linkperson" label="负责人">
        <el-input v-model="form.linkperson" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="linkperson_phone" label="负责人电话">
        <el-input v-model="form.linkperson_phone" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="address" label="所属地区">
        <el-input v-model="form.address" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="descri" label="班级详情">
        <el-input type="textarea" v-model="form.descri" size="mini" style="width: 300px"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
        <el-button size="mini" @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { saveCustomerClass, getCustomerClassDetail } from '@/api/customerClass';
import { getObjTypeList } from '@/api/objType';
export default {
  name: 'customerAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false,
      required: true
    },
    title: {
      type: String,
      default: '新增班级'
    },
    userId: {
      type: Number,
      default: null,
    }
  },
  data() {
    return {
      localVisible: this.showDialog,
      objTypes: [], //课程类型
      form: {
        name: '',
        linkperson: '',
        linkperson_phone: '',
        address: '',
        fk_t_obj_type_id: '',
        descri: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入班级名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
        ],
        linkperson: [
          { required: true, message: '请输入负责人名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
        ],
        linkperson_phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
          //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        fk_t_obj_type_id: [
          { required: true, message: '请选择课程类型', trigger: 'blur' },
        ],
      }
    };
  },

  watch: {
    // 监听父组件传递的 visible，同步到本地变量
    showDialog(newVal) {
      this.localVisible = newVal;
      this.resetForm(); // 先重置表单
      this.getObjTypeList();
      if (this.userId != null) {
        this.getCustomerClassDetail();
      }
    },
    // 监听本地变量的变化，通知父组件更新 visible
    localVisible(newVal) {
      if (newVal !== this.showDialog) {
        this.$emit('update:showDialog', newVal);
      }
    }
  },
  methods: {
    // 获取课程信息
    async getObjTypeList() {
      const queryParams = { type: this.type }
      const { data } = await getObjTypeList(queryParams)
      // 转换逻辑
      const objTypeList = data.map(item => ({
        value: item.id, // 将 id 映射为 value
        label: item.name // 将 name 映射为 label
      }));
      this.objTypes = objTypeList
    },
    btnOk() {
      this.$refs.addCustomer.validate(async isOk => {
        if (isOk) {
          let msg = '新增'

          if (this.userId) {
            msg = '编辑'
            console.log('编辑');

            await saveCustomerClass({ ...this.form, id: this.userId })
          } else {
            //调用接口
            await saveCustomerClass(this.form)
          }

          // 通知父组件更新
          this.$emit('submit')
          //成功提示
          this.$message.success(`${msg}班级成功`)
          //重置表单
          this.handleClose()
        }
      })
    },
    handleClose() {

      this.localVisible = false; // 修改本地变量
      this.resetForm(); // 清理表单数据
    },
    resetForm() {
      if (this.$refs.addCustomer) {
        this.$refs.addCustomer.resetFields(); // 重置表单
        this.form = {
          name: '',
          linkperson: '',
          linkperson_phone: '',
          address: '',
          fk_t_obj_type_id: '',
          descri: ''
        }
      }
    },
    //获取客户详情
    async getCustomerClassDetail() {
      this.form = await getCustomerClassDetail({ id: this.userId })
    }


  }
};
</script>

<style scoped>
/* 可以在这里添加样式 */
</style>